package com.example.quartz.provider.sms.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.quartz.annotation.TransactionalService;
import com.example.quartz.consumer.sms.dto.QuartzTaskDTO;
import com.example.quartz.module.sms.QuartzTask;
import com.example.quartz.provider.sms.mapper.QuartzTaskMapper;
import com.example.quartz.provider.sms.service.QuartzTaskService;
import com.example.quartz.utils.SchedulerUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: wlp
 * @description: 描述：定时任务 服务实现类
 * @projectName: QuartzTaskServiceImpl
 * @date: 2019/10/11
 * @version: V1.0.0
 */
@Slf4j
@TransactionalService
public class QuartzTaskServiceImpl extends ServiceImpl<QuartzTaskMapper, QuartzTask> implements QuartzTaskService {

    @Resource
    private Scheduler scheduler;

    @Resource
    private QuartzTaskMapper quartzTaskMapper;

    public void init() {
        EntityWrapper<QuartzTask> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", Boolean.FALSE);
        wrapper.eq("quartz_status", 0);
        List<QuartzTask> quartzTaskList = quartzTaskMapper.selectList(wrapper);
        for (QuartzTask quartzTask : quartzTaskList) {
            CronTrigger cronTrigger = SchedulerUtils.getCronTrigger(scheduler, quartzTask.getId());
            if (cronTrigger == null) {
                SchedulerUtils.createScheduleJob(scheduler, quartzTask);
            } else {
                SchedulerUtils.updateScheduleJob(scheduler, quartzTask);
            }
        }
    }

    @Override
    public List<QuartzTask> listQuartzTask(QuartzTaskDTO quartzTaskDTO) {
        return null;
    }
}